package com.itheima.home_work.test2;

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        /**
         * 文件合并
         */

        // 1. 获取5个文件的路径
        String[] files = new String[5];

        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\data\\ticks\\12. 打印流.mp4", true));

        // 2. 分5步写入缓冲流
        for (int i = 0; i < 5; i++) {
            try {
                appendCopy("D:\\data\\ticks\\12. 打印流.mp4." + i, bos);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 3. 关闭流
        bos.close();

    }

    // 追加写入操作
    public static void appendCopy(String sourcePath, BufferedOutputStream bos) throws IOException {
        // 1. 创建流
        final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourcePath));

        // 2. 读取并写入流
        int len = -1;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        // 3. 关闭流
        bis.close();
    }
}
